package com.mp.ipm2020.admin.controller;

import cn.hutool.core.date.DateTime;
import com.mp.ipm2020.common.api.CommonPage;
import com.mp.ipm2020.common.api.CommonResult;
import com.mp.ipm2020.mbg.model.DeviceInfo;
import com.mp.ipm2020.mbg.model.Menu;
import com.mp.ipm2020.mbg.service.DeviceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Auther adidasshe@mp.cn
 * <p>
 * 设备信息 Controller
 */
@RestController
@Api(tags = "设备信息")
@RequestMapping("/device_info")
public class DeviceInfoController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    @ApiOperation("添加设备")
    @PostMapping
    public CommonResult create(@RequestBody DeviceInfo deviceInfo) {
        deviceInfo.setCreateTime(new Date());
        int count = deviceInfoService.create(deviceInfo);
        if (count == 1) {
            return CommonResult.success(count);
        } else if (count == 2) {
            return CommonResult.failed("设备已存在");
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改设备")
    @PutMapping("/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody DeviceInfo deviceInfo) {
        deviceInfo.setId(id);
        deviceInfo.setUpdateTime(new Date());
        boolean count = deviceInfoService.updateById(deviceInfo);
        if (count == true) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除设备")
    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable Long id) {
        boolean count = deviceInfoService.removeById(id);
        if (count == true) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("分页查询设备列表")
    @GetMapping()
    public CommonResult<CommonPage<DeviceInfo>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<DeviceInfo> list = deviceInfoService.list(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }
}
